package com.mxpio.erp.inventory.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.mxpio.erp.common.inventory.entity.Warehouse;
import com.mxpio.erp.common.inventory.service.WarehouseService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

@Tag(name = "WarehouseController", description = "仓库管理接口")
@RestController
@RequestMapping("/erp/inventory/wh/")
public class WarehouseController {
	
	@Autowired
	private WarehouseService warehouseSerivce;

	@GetMapping("list")
	@Operation(summary = "仓库列表", description = "获取仓库列表", method = "GET")
	public Result<List<Warehouse>> list(Criteria criteria) throws UnsupportedEncodingException {
		List<Warehouse> warehouses = warehouseSerivce.list(Warehouse.class, criteria);
		return Result.OK(warehouses);
	}
	
	@GetMapping("page")
	@Operation(summary = "仓库列表", description = "获取仓库列表", method = "GET")
	public Result<Page<Warehouse>> page(Criteria criteria, Integer pageSize, Integer pageNo) throws UnsupportedEncodingException {
		Pageable pageAble = PageRequest.of(pageNo-1, pageSize);
		Page<Warehouse> page = warehouseSerivce.listPage(Warehouse.class, pageAble, criteria);
		return Result.OK(page);
	}
	
	@GetMapping("list/{whCode}")
	@Operation(summary = "根据whCode获取仓库", description = "根据whCode获取仓库", method = "GET")
	public Result<Warehouse> getById(@PathVariable(name = "whCode", required = true) String whCode) {
		Warehouse warehouse = warehouseSerivce.getById(Warehouse.class, whCode);
		return Result.OK(warehouse);
	}
	
	@PostMapping("add")
	@Operation(summary = "新增仓库", description = "新增仓库", method = "POST")
	public Result<Warehouse> add(@RequestBody Warehouse warehouse) {
		warehouseSerivce.save(warehouse);
		return Result.OK(warehouse);
	}
	
	@PutMapping("edit")
	@Operation(summary = "编辑仓库", description = "编辑仓库（全量）", method = "PUT")
	public Result<Warehouse> edit(@RequestBody Warehouse warehouse) {
		warehouseSerivce.update(warehouse);
		return Result.OK(warehouse);
	}
	
	@DeleteMapping("remove/{whCodes}")
	@Operation(summary = "删除仓库", description = "删除仓库", method = "DELETE")
	public Result<Warehouse> remove(@PathVariable(name = "whCodes", required = true) String whCodes) {
		String whCode[] = whCodes.split(",");
		for(String key : whCode){
			warehouseSerivce.delete(Warehouse.class, key);
		}
		return Result.OK();
	}
	
}
